//	--------------------------------------------------------------------
//	Copyright(C) 2006,2007 Zhao Yukun. All rights reserved.
//	This file is a part of the Xeres system.
//	Xeres 2007
//	Contact : xeres.engine@gmail.com
//	$(reservedInfo)
//	$(reservedDeclaration)
//	--------------------------------------------------------------------
///	@file	<DirectInputAdapter.h>
///	@path	~/src/appear/xwin/
///	@date	2007/11/06
///	@desc	Use Direct Input as adapter.

#pragma once

#include "xeres.h"

// Direct Input
#define DIRECTINPUT_VERSION 0x0800
#include <dinput.h>

#include "lib/utilities/time.h"

#include "appear/app/input_handler.h"
#include "appear/app/ticker.h"
#include "appear/app/message_filter.h"

namespace xeres {

	/*!
		\class	DirectInputAdapter
		\brief	Use Direct Input as input adapter.
	*/
	class DirectInputAdapter : public MessageFilter , public Ticker
	{
	public:
		
		/// \ctor
		DirectInputAdapter( InputHandler * inputHandler );

		/// \dtor
		virtual ~DirectInputAdapter( void );

	//@ interface

		/*!
			\brief		Filter messages,
						return true to stop next level,
						otherwise engine will search next filter.
		*/
		virtual bool Filter( HWND hWnd , UINT message , WPARAM wParam , LPARAM lParam );

		/*!
			\brief		Tick update call.
		*/
		virtual void Tick( Engine& engine );

	//@ local

	private:

		/*!
			\brief		Initialize direct input device.
		*/
		void InitializeDevice( HWND hWnd );

		/*!
			\brief		Acquire input device.
		*/
		void Acquire( void );

		/*!
			\brief		Unacquire input device.
		*/
		void Unacquire( void );

		// process input data
		void ProcessKeyInput( void );
		void ProcessMouseInput( void );

		// data

		// input handler
		InputHandler *			m_inputHandler;

		// direct input 8 interface
		IDirectInput8 *			m_dinput8;
		IDirectInputDevice8 *	m_keyboard8;
		IDirectInputDevice8 *	m_mouse8;

		// input state
		uint32					m_inputState;
		POINTS					m_lastPos;

		Time					m_lastUpdate;

		Time					m_accuracy;

		// state
		bool					m_paused;
	};

} // namespace xeres
